#include<iostream>
#include<unistd.h>
#include<cstdio>
#include<sys/wait.h>
#include<sys/types.h>

using namespace std;

int main()
{

    pid_t pid=fork();
    if(pid==-1)
    {
        perror("fork");
        return -1;
    }
    if(pid==0)
    {
        cout<<"i am is child precess, pid : "<<getpid<<endl;
        int cnt=5;
        while(cnt--)
        {
            cout<<"你不会要过来"<<endl;
            sleep(1);
        }

        //exec是程序替换
        //他的家族有execvpe,execv,execve;   v代表的是数组
        //         execl,execlpe,execle;   l代表的是例表   
        cout<<"你被我替换了"<<endl;
        char* const myargv[]={"ls","-a","-l",NULL};
        execvp("ls",myargv);
        cout<<getpid()<<"能回来吗？"<<endl;
    }
    else
    {
        while(true)
        {
            cout<<"我在等待子进程"<<endl;
            sleep(1);
            int status=0;
            int info=waitpid(-1,nullptr,0);
            if(info==pid)
            {
                cout<<"等待进程结束"<<endl;
                sleep(3);
                break;        
            }
        }
    }

    return 0;
}